#include <bits/stdc++.h>

using namespace std;
int n;

int a[10];
int b[10];

int solve(int *arrA, int *arrB) // a洗头顺序，b是理发得顺序
{
    int washTime[10];
    int last = 0;
    for (int i = 0; i < n; ++i)
    {
        int idxa = arrA[i];
        int time = a[idxa];
        washTime[idxa] = (last += time);
    }
    last = 0;
    int finishTime[10];
    for (int i = 0; i < n; ++i)
    {
        int idxB = arrB[i];
        int time = b[idxB];
        finishTime[idxB] = (last = max(washTime[idxB], last) + time);
    }
    return last;
}

int searchTime()
{
    int arrA[] = {0, 1, 2, 3, 4, 5, 6};
    int arrB[] = {0, 1, 2, 3, 4, 5, 6};
    int ans = 0x3f3f3f3f;
    do
    {
        // do
        // {
            ans = min(ans, solve(arrA, arrA));
        // } while (next_permutation(arrB, arrB + n));
    } while (next_permutation(arrA, arrA + n));
    return ans;
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n;
        for (int i = 0; i < n; ++i)
        {
            cin >> a[i] >> b[i];
        }
        cout << searchTime() << endl;
    }
    // system("pause");
    return 0;
}
